//
//  DoctorInfoCollectionView.m
//  QuickCure
//
//  Created by 凉气的Mac on 16/9/13.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "DoctorInfoCollectionView.h"

#import "DoctorInfoCollectionViewCell.h"

#import "FocusDoctorModel.h"

static NSString *reuserId = @"doctorInfo";

@interface DoctorInfoCollectionView ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,weak) UICollectionViewFlowLayout *flow;

@property(nonatomic,strong)  NSArray *modelArray;
//定时器
@property (nonatomic, strong) CADisplayLink *displayLink;
//collectionview的contentoffset
@property (nonatomic, assign) CGFloat offsetX;

@end

@implementation DoctorInfoCollectionView

-(void)setModelArray:(NSArray *)modelArray{
    
    _modelArray = modelArray;
    
    [self reloadData];
}


-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
    
    UICollectionViewFlowLayout *flow = (UICollectionViewFlowLayout *)layout;
    
    self.flow = flow;
    
    if (self = [super initWithFrame:frame collectionViewLayout:flow]) {
        
        
        [self setupUI];
        //设置定时器，使collectionview滚动
        [self display];
    }
    
    return self;
}

-(void)setupUI{
    
    [FocusDoctorModel focusDoctorModelWithSuccess:^(NSArray *modelArray) {
        
        self.modelArray = modelArray;
        
    } error:^{
        
        NSLog(@"error");
        
    }];
    
    [self registerClass:[DoctorInfoCollectionViewCell class] forCellWithReuseIdentifier:reuserId];
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.delegate = self;
    
    self.dataSource = self;
    
}

-(void)layoutSubviews{
    
    [super layoutSubviews];
    
    self.bounces = NO;
    
    self.flow.itemSize = CGSizeMake(sWidth/3, self.bounds.size.height);
    
    self.flow.minimumLineSpacing = 0;
    
    self.flow.minimumInteritemSpacing = 0;
    
    self.flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.showsHorizontalScrollIndicator = NO;
    
    
}

#pragma mark --
#pragma mark --collectuonView数据源
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.modelArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    DoctorInfoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuserId forIndexPath:indexPath];
    
    cell.model = self.modelArray[indexPath.item];
    
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    FocusDoctorModel *model = self.modelArray[indexPath.item];
    
    NSNotification *notification = [NSNotification notificationWithName:@"clickCell" object:nil userInfo:@{@"data":model}];
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center postNotification:notification];
    
    
}


- (void)display {
    
    CADisplayLink *display = [CADisplayLink displayLinkWithTarget:self selector:@selector(keepScroll)];
    
    [display addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)keepScroll {
    
    if(self.offsetX >= (self.contentSize.width - [UIScreen mainScreen].bounds.size.width)) {
        
        //滚动到头时，回到最左边
        self.offsetX = 0;
    }
    
    self.offsetX += 0.2;
    
    [self setContentOffset:CGPointMake(self.offsetX, 0)];
}


-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
    self.offsetX = scrollView.contentOffset.x;
}

@end

